<?php

/*
 *************************************************************************
 * This class written by : MaaSTaaR <MaaSTaaR(at)hotmail.com>
 *
 * You can control in template with Smarty : http://smarty.php.net
 *
 * Version : 1.1 Special version for MySmartBB
 *
 * License : GNU GPL <http://www.opensource.org/licenses/gpl-license.php>
 *************************************************************************
 */

        class STControl
    {
                 var $tdir;     #Template dir
                 var $mainpath; #main file path
                 var $editpath; #edit file path
                 var $addpath;  #add file path
                 var $delpath;  #delete file path
                 var $starteditpath; #start edit file path
                 var $defualtex;     #defualt extension .. example (.tpl)


                /*
                GetTemplate :
                        this function get template name and put it in list
                */

        function GetTemplate()
        {
                          if (is_dir($this->tdir))
            {
                                   if ($dh = opendir($this->tdir))
                {
                                    echo '<br /><div align="center">';
                                    echo '<table class="border" summary="جدول" cellspacing="1" width="50%">
                                                    <tr>
                                                      <td class="row2" width="60%">اسم القالب</td>
                                                      <td class="row2" width="20%">تحرير</td>
                                                      <td class="row2" width="20%">حذف</td>
                                                    </tr>';

                                    while (($file = readdir($dh)) !== false)
                    {
                                             if ($file == "." or $file == ".." or $file == ".htaccess")
                        {
                                                      continue;
                                             }

                        echo '<tr>
                                                              <td class="row1">' . $file . '</td>
                                                              <td class="row1"><a href="' . $this->editpath . '&file=' . $file . '&path=' . htmlspecialchars($_GET['path']) . '">تحرير</a></td>
                                                              <td class="row1"><a href="' . $this->delpath . '&file=' . $file . '&path=' . htmlspecialchars($_GET['path']) . '">حذف</a></td>
                                                              </tr>';

                                    }

                                    echo '</table>';
                                    echo '</div><br />';

                    closedir($dh);
                                   }
                          }
                 }

        /*
                EditTemplate :
                        edit form get one parameter and usualy it $D1 , this parameter = file name
                */

                 function EditTemplate($D1,$path)
        {
                global $SF;

                if (!file_exists($this->tdir . $D1))
                {
                        $SF->error('الملف المطلوب غير موجود');
                }

                          $fp = file($this->tdir . $D1);
                          $ln = count($fp);

                          echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">';
                          echo '<div dir="rtl" align="center">تحرير القالب ' . $D1 . '</div>';
                          echo '<form method="POST" action="' . $this->starteditpath . '&filename=' . $D1 . '&path=' . $path . '">';
                          echo '<p align="center"><textarea rows="30" name="S1" cols="70" dir="ltr">';

            for ($i=0;$i<=$ln;$i++)
            {
                                   echo htmlspecialchars($fp[$i]);
                          }

            echo '</textarea>';
                          echo '<br><input type="submit" value="موافق" name="B1" style="font-family: Tahoma; font-size: 8pt"></p>';
                 }

                /*
                StartEdit :
                        this function will change template code and get one parameter , it is usualy $S1
                        and $S1 = template code
                */

                 function StartEdit($S1,$path)
        {
                          global $filename;

                          $S1 = stripslashes($S1);
                          $fp = fopen($this->tdir . $filename,'w+');
                          $fw = fwrite($fp,$S1);

                          if ($fw)
            {
                                   echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">';
                                   $this->msg("حُرِّرَ القالب بنجاح");
                                   $this->goto($this->mainpath . '&path=' . $_GET['path'],'2');
                          }
                 }

        /*
                AddTemplate :
                        this function will show add form
                */


				 function AddTemplate()
					{ 
						  include('config.php');
                          global $DB;

                          $getstyle_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "style ORDER BY id DESC");

                          echo '<form method="POST" action="' . $this->addpath . '">';
                          echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">';
                          echo '<p dir="rtl" align="center">اسم القالب : ';
                          echo '<input name="T1" type="text" style="font-family: Tahoma; font-size: 8pt">';
                          echo '<br>القالب يعود للستايل التالي : ';
                          echo '
                          <select size="1" name="D1" style="font-family: Tahoma; font-size: 8pt" dir="rtl">
                          ';
                          echo '<option selected value="no">[الستايل]</option>';

            while ($getstyle_row = $DB->sql_fetch_array($getstyle_query))
            {
                                   echo '<option value="' . $getstyle_row['template_path'] . '">' . $getstyle_row['style_title'] . '</option>';
                          }

                          echo '</select><p align="center"><textarea rows="30" name="S1" cols="70" dir="ltr" style="font-family: Tahoma; font-size: 10pt">';
                          echo '</textarea>';
                          echo '<br><input type="submit" value="موافق" name="B1" style="font-family: Tahoma; font-size: 8pt"></p>';
                 }

                /*
                StartAdd :
                        this function will add template
                */

                 function StartAdd()
        {
                          global $T1,$S1,$_POST;

                          $S1 = stripslashes($S1);
                          $fp = fopen("../" . $_POST['D1'] . "/" . $T1 . $this->defualtex,'w');
                          $fw = fwrite($fp,$S1);

                          if ($fw)
            {
                                   echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">';
                                   $this->msg("أُضيفَ القالب بنجاح");
                                   $this->goto("template.php?edit=1&form=1&path=" . $_POST['D1'],'2');
                          }
                 }

                /*
                DeleteTemplate :
                        this function will kll template
                        by: bokhalifa -- bo@ib4arab.com
                */
                 function DeleteTemplate($D1,$path)
            {
                          $fw = unlink("../" . $path . "/" . $D1 . "");

                if ($fw)
                {
                                   echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">';
                                   $this->msg("حُذِفَ القالب بنجاح");
                                   $this->goto($this->mainpath . "&path=" . $path,'2');
                          }
                 }

                 function msg_die($msg)
            {
                          die('<p align="center" dir="rtl">' . $msg . '</p>');
                 }

                 function msg($msg)
            {
                          echo('<p align="center" dir="rtl">' . $msg . '</p>');
                 }

                  function goto($site,$m)
            {
                          echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"$m; URL=$site\">\n";
                 }
        }

?>